package com.hd.service.impl;

import com.googlecode.ehcache.annotations.KeyGenerator;
import com.googlecode.ehcache.annotations.Property;
import com.hd.dao.BaseDao;
import com.hd.entity.Person;
import com.hd.service.PersonService;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 注解缓存 Cacheable注解负责将方法的返回值加入到缓存中 CacheEvict注解负责清除缓存(它的三个参数与@Cacheable的意思是一样的)
 * 
 * @see ----------------------------------------------------------------------------------------------------------
 * @see value------缓存位置的名称,不能为空,若使用EHCache则其值为ehcache.xml中的<cache name=
 *      "myCache"/>
 * @see key--------缓存的Key,默认为空(表示使用方法的参数类型及参数值作为key),支持SpEL
 * @see condition--只有满足条件的情况才会加入缓存,默认为空(表示全部都加入缓存),支持SpEL
 * @see allEntries--清空缓存位置中所有缓存
 * @see ----------------------------------------------------------------------------------------------------------
 * @see 该注解的源码位于spring-context-3.2.4.RELEASE-sources.jar中
 * @see Spring针对Ehcache支持的Java源码位于spring-context-support-4.2.3.RELEASE-sources.jar中
 * @see 具体参看：https://blog.csdn.net/u014201191/article/details/46481819
 * @see ----------------------------------------------------------------------------------------------------------
 */

@Service("personService")
public class PersonServiceImpl implements PersonService {
	@Autowired
	private BaseDao<Person, String> personDao;

	/**
	 * 插入数据
	 */
	// @CacheEvict(value="myQueryCache",allEntries=true)
	@Override
	public Serializable save(Person person) {
		// 生成主键id
		// person.setId(UUID.randomUUID().toString());
		person.setId(geNextPrimaryKey());
		person.setCreateTime(new Date());
		return personDao.save(person);
	}

	// 查询最大主键
	private String geNextPrimaryKey() {
		Object maxValue = personDao.getMaxValue(Person.class, "id");
		if (maxValue != null) {
			Long value = Long.valueOf((String) maxValue) + 1L;
			return value.toString();
		}
		return "1";
	}

	/**
	 * 删除数据
	 */
	// @CacheEvict(value="myQueryCache",allEntries=true)
	@Override
	public void delete(String id) {
		Person person = get(id);
		personDao.delete(person);
	}

	/**
	 * 删除数据
	 */
	// @CacheEvict(value="myQueryCache",allEntries=true)
	@Override
	public void deleteById(String id) {
		String hql = "delete from Person where id=?";
		personDao.execute(hql, id);
	}

	/**
	 * 更新数据
	 */
	// @CacheEvict(value="myQueryCache",key="'get'+#person.id")
	// @CacheEvict(value="myQueryCache",allEntries=true)
	@Override
	public void update(Person person) {
		Person vo = get(person.getId());
		person.setCreateTime(vo.getCreateTime());
		personDao.update(person);
	}

	/**
	 * 取一条数据
	 */
	// @Cacheable(value="myQueryCache",key="'get'+#id")
	@Override
	public Person get(String id) {
		// TODO Auto-generated method stub
		return personDao.get(Person.class, id);
	}

	/**
	 * 通过用户名查询数据
	 */
	// @Cacheable(value="myQueryCache",key="'get'+#name")
	@Override
	public List<Person> getByName(String name) {
		String hql = "from Person where name like ?";
		return personDao.find(hql, "%" + name + "%");
	}

	/**
	 * 查询所有数据
	 */
	// @Cacheable(value="myQueryCache",key="1")
	@Override
	public List<Person> findAll() {
		String hql = "from Person";
		return personDao.find(hql);
	}

	/**
	 * 查询总记录数
	 */
	// @Cacheable(value="myQueryCache",key="2")
	@Override
	public int findTotal() {
		String countHql="select count(*) from Person ";
		int total=Integer.parseInt(personDao.getCount(countHql).toString());
		return total;
	}

	/**
	 * 分页
	 */
	/*
	 * @com.googlecode.ehcache.annotations.Cacheable(cacheName =
	 * "myQueryCache",keyGenerator = @KeyGenerator ( name =
	 * "com.hd.util.result.PageKeyGenerator", properties = @Property(
	 * name="includeMethod", value="false")))
	 */
	@Override
	public void selectDataGrid(PageInfo info) {
		String hql = "from Person";
		Map<String, Object> condition = info.getCondition();
		List<Object> params = new ArrayList<Object>();
		List<Person> personList = personDao.findPage(hql, info.getNowpage(), info.getPagesize(), params);
		String countHql="select count(*) "+hql;
		int total=Integer.parseInt(personDao.getCountByList(countHql,params).toString());
		info.setTotal(total);
		info.setRows(personList);
	}

	@Override
	public List<Person> findList(String hql, Integer page, Integer pageSize, List<Object> params) {
		List<Person> personList = personDao.findPage(hql, page, pageSize, params);
		return personList;
	}

	@com.googlecode.ehcache.annotations.Cacheable(cacheName = "myQueryCache", keyGenerator = @KeyGenerator(name = "com.hd.util.result.PageKeyGenerator", properties = @Property(name = "includeMethod", value = "false")))
	@Override
	public List<Person> list(Integer page, Integer pageSize, Map<String, Object> condition) {
		String hql = "from Person where 1=1";
		List<Object> params = new ArrayList<Object>();
		if (StringUtils.isNotNull(condition.get("name"))) {
			hql += " and name=?";
			params.add(condition.get("name"));
		}
		if (StringUtils.isNotNull(condition.get("age"))) {
			hql += " and age=?";
			params.add(condition.get("age"));
		}
		List<Person> personList = personDao.findPage(hql, page, pageSize, params);
		return personList;
	}

	// @Cacheable(value="myQueryCache",key="41")
	@Override
	public int findForSize(String hql, List<Object> params) {
		int total = personDao.findByList(hql, params).size();
		return total;
	}
}
